﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Diagnostics;

namespace Royaumes_Libres.Database
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks>Patrick Chabot</remarks>
    public class cBuildings
    {
        protected int pPkKingdom;
        protected int pPkBuilding;
        protected string pName;
        protected int pMana;
        protected int pTimber;
        protected int pOre;
        protected int pWorkUnite;
        protected int pWorkMax;
        protected decimal pBonusMana;
        protected decimal pBonusTimber;
        protected decimal pBonusOre;
        protected decimal pBonusFood;
        protected decimal pBonusPopulation;
        protected bool pUniqueBuild;
        protected string pImgUrl;

        public int PkBuilding { get { return this.pPkBuilding; } }
        public string Name { get { return this.pName; } }
        public int Timber { get { return this.pTimber; } }
        public int Ore { get { return this.pOre; } }
        public int Mana { get { return this.pMana; } }
        public string ImgUrl { get { return this.pImgUrl; } }
        public bool UniqueBuild { get { return this.pUniqueBuild; } }
        public int WorkUnite { get { return this.pWorkUnite; } }

        /// <summary>
        /// Démarre la construction du buiding
        /// </summary>
        /// <remarks>Patrick Chabot</remarks>
        public void StartBuild()
        {
            SqlCommand cmd = cSQL.GetDbJeuCommand();

//#if(Debug)
            Debug.Write("PkKingdon: " + this.pPkKingdom);
            Debug.Write("PkBuilding: " + this.pPkBuilding);
//#endif
            try
            {
                cmd.Connection.Open();
                cmd.CommandText = "cde.spStartBuild";
                cmd.Parameters.Add(cSQL.Param("@PkKingdom", this.pPkKingdom, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@PkBuilding", this.pPkBuilding, SqlDbType.Int));
                cmd.ExecuteNonQuery();

                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                cmd.Connection.Close();
                throw new Exception("cBuildings.StartBuild(): " + ex.Message);
            }
        }

        /// <summary>
        /// Retourne le control ucBuiding
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        /// <remarks>Patrick Chabot</remarks>
        public ucBuilding GetControl(Page page)
        {
            //if (this.pUniqueBuild && (this.pQuantity == 1)) { return null; }

            ucBuilding uc = (ucBuilding)page.LoadControl("ucBuilding.ascx");
            uc.Building = this;
            return uc;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <remarks>Patrick Chabot</remarks>
        protected cBuildings() { }

        /// <summary>
        /// Liste les Buildings.
        /// </summary>
        /// <param name="PkWorld"></param>
        /// <param name="PkKingdom"></param>
        /// <returns></returns>
        /// <remarks>Patrick Chabot</remarks>
        public static cBuildings[] GetBuildings(int PkKingdom)
        {
            List<cBuildings> lstBuildings = new List<cBuildings>();
            SqlCommand cmd = cSQL.GetDbJeuCommand();
            cBuildings building;

            try
            {
                cmd.Connection.Open();
                cmd.CommandText = "cde.spGetBuildings";
                cmd.Parameters.Add(cSQL.Param("@PkKingdom", PkKingdom, SqlDbType.Int));
                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    building = new cBuildings();

                    building.pPkKingdom = PkKingdom;
                    building.pPkBuilding = dr.GetInt32(0);
                    building.pName = dr.GetString(1);
                    building.pMana = dr.GetInt32(2);
                    building.pTimber = dr.GetInt32(3);
                    building.pOre = dr.GetInt32(4);
                    building.pWorkUnite = dr.GetInt32(5);
                    building.pWorkMax = dr.GetInt32(6);
                    building.pBonusMana = dr.GetDecimal(7);
                    building.pBonusTimber = dr.GetDecimal(8);
                    building.pBonusOre = dr.GetDecimal(9);
                    building.pBonusFood = dr.GetDecimal(10);
                    building.pBonusPopulation = dr.GetDecimal(11);
                    building.pUniqueBuild = dr.GetBoolean(12);
                    building.pImgUrl = dr.GetString(13);

                    lstBuildings.Add(building);
                }
                cmd.Connection.Close();
                return lstBuildings.ToArray();
            }
            catch(Exception ex)
            {
                cmd.Connection.Close();
                throw new Exception("cBuildings.GetBuildings(): " + ex.Message);
            }
        }
    }
}